define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Myagent, $stateParams, GoDialog, $timeout, GoPage, $window,$ionicScrollDelegate) {
		
		//画面加载。3是授权中心，4是加盟店
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.title = $stateParams.isAgent=='false'?"加盟店":"授权中心";
			$scope.init_data();
		}, false);

		//初始化
		$scope.init_data = function() {
			Myagent.my_agent(1, function(data) {
				$scope.datas = data;
				scrollToRight();
			})
		}
		
		//将按钮栏滚动到最有边
		function scrollToRight(){
			$timeout(function(){
				$ionicScrollDelegate.$getByHandle('btnScroll').scrollTo(300);//值可以取大点，滚动到最有边就不会再滚了
			},500)
		}
		
		//按钮事件
		$scope.switchEvent = function(buttonId,store_id) {
			switch(buttonId) {
				case 1://查看销量
					{ 
						GoPage.go_states("myagentsale", {"store_id": store_id});
						break;
					};
				case 2://授信还款
					{ 
						GoPage.go_states("creditrepayview", {id: store_id});
						break;
					};
				case 3://编辑
					{ 
						$scope.onshow_add(store_id);
						break;
					};
				case 4://发红包
					{ 
						GoPage.go_states("distributor_redbag",{
							"store_id":store_id,
							'advance':$scope.datas.member_advance
						});
						break;
					};
				case 5://删除
					{ 
						deleteMyagent(store_id);
						break;
					};		
			}
		}
		
		//删除我的授权中心
		function deleteMyagent(store_id) {
			GoDialog.confirm("提示","确定要删除吗？",function(){
				Myagent.delete_agent(store_id, function(data) {
					if(data.success) {
						GoDialog.toast("刪除成功");
						$scope.init_data();
					} else{
						GoDialog.alert("提示",data.msg);
					}
				});
			})
		}
		
		//跳转到店铺首页
		$scope.onshow_first = function(store_id) {
			GoPage.go_states('first', { 'store_id': store_id });
		}
		//添加授权
		$scope.onshow_add = function(store_id){
			GoPage.go_states('myagent_add',{ "store_id": store_id,'isAgent':$stateParams.isAgent});
		}
		
		
		/***************上下拉刷新******************/
		$scope.nPage = 1;
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Myagent.my_agent(1, function(data) {
				$scope.datas = data;
				$scope.nPage = 1;
				scrollToRight();
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Myagent.my_agent($scope.nPage + 1, function(data) {
				if(data != null) {
					if($scope.datas.store != undefined) {
						$scope.datas.store = $scope.datas.store.concat(data.store);
						$scope.nPage += 1;
						scrollToRight();
					} else {
						$scope.datas = data;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		/***************上下拉刷新******************/
		

	}

	controller.$inject = ['$scope', 'Myagent', '$stateParams', 'GoDialog', '$timeout', 'GoPage', '$window','$ionicScrollDelegate'];

	return app.registerController("myagentctrl",controller);
});